<?php

namespace app\api\service\shop;

use app\admin\model\SearchHistory;
use think\Collection;
use think\Exception;

class SearchService
{

    private CarbonFactorService $carbonFactorService;
    private AccountTemplateService $accountTemplateService;

    public function __construct()
    {
        $this->carbonFactorService = new CarbonFactorService();
        $this->accountTemplateService = new AccountTemplateService();
    }

    /**
     * 搜索
     * @param int $typeId 类别ID
     * @param string $content 搜索文本
     * @param int $page 页码
     * @param int $limit 每页展示条数
     * @param int $userId 用户ID
     * @return array|null
     * @throws Exception
     */
    public function search(int $typeId, string $content, int $page, int $limit, int $userId): ?array
    {
//        if (SearchHistory::where(['user_id' => $userId, 'word' => $content])->count('id') == 0)
//            SearchHistory::create(['user_id' => $userId, 'word' => $content]);
        switch ($typeId) {
            case 1:
                return $this->accountTemplateService->getTemplateList($page, $limit, $content);
            case 2:
                return $this->carbonFactorService->getFactorList([], $userId, $page, $limit, $content);
            default:
                return null;
        }
    }

    /**
     * 获取搜索历史
     */
    public function getSearchHistory(int $userId): ?Collection
    {
        return SearchHistory::where('user_id', $userId)->select();
    }

}